package com.hshx.service.impl;

import com.hshx.service.IMessageProduceService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class MessageProduceServiceImpl implements IMessageProduceService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    public void send(String exchange,String routingKey,Object msg){
        this.rabbitTemplate.convertAndSend(exchange,routingKey,msg);
    }
    /**
     * 直连
     * @param msg
     */
    @Override
    public void sendMessageDirect(String msg) {
        this.send("direct","rk",msg);
    }
    /**
     * 广播
     * @param map
     */
    @Override
    public void sendMessageFanout(Map<String, Object> map) {
        this.send("fanout","",map);
    }
    /**
     * 主题(通配符)
     * @param msg
     */
    @Override
    public void sendMessageTopic(String msg) {
        this.send("topic","log.admin","log.admin"+msg);
        this.send("topic","a.b.c.log.d","a.b.c.log.d"+msg);
        this.send("topic","a.log","a.log"+msg);
    }

    @Override
    public void sendDelayedMessage(String msg) {
        rabbitTemplate.convertAndSend("delay.exchange","delay.routkey", msg, message -> {
            // 设置过期时间
            message.getMessageProperties().setDelay(5000);
            return message;
        });
    }

}
